/* Hello World Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/

#include <stdio.h>
#include <string.h>
// #include "sdkconfig.h"
// #include "freertos/FreeRTOS.h"
// #include "freertos/task.h"
// #include "esp_task_wdt.h"
#include "esp_system.h"
#include "esp_chip_info.h"
#include "esp_mac.h"
#include "esp_err.h"
#include "esp_flash.h"
#include "esp_log.h"
#include "esp_ota_ops.h"
#include "main.h"
#include "generic_gpio.h"
#include "nvs_value.h"
#include "ws2812.h"
#include "lamp_ctrl.h"
#include "touch_board.h"
#include "uart_events.h"
#include "gatt_server.h"
#include "h03_config.h"

static const char *TAG = "[MAIN]";

char *DEVICE_NAME = "H03";

system_state_t system_state = {
    .time_sync = 0,
    .wifi = wifi_WAITING,
};
device_attr_t device_attr;

void restart_os(void)
{
	vTaskDelay(100 / portTICK_PERIOD_MS);
	esp_restart();
}

void printf_buf(char *tmp_name, uint8_t *tmp_buf, uint8_t tmp_len)
{
	printf("%s: ", tmp_name);
	for(uint16_t i = 0; i < tmp_len; i++)
	{
		printf("%02x ",*(tmp_buf + i));
	}
	printf("\r\n");
}

void printf_char(char *tmp_name, uint8_t *tmp_buf, uint8_t tmp_len)
{
    printf("%s:\n", tmp_name);
    for(int i = 0; i < tmp_len; i++){
        printf("%c", tmp_buf[i]);
    }
    // printf("\n");
}

void system_get_info(void)
{
    int ver_big;
    int ver_middle;
    int ver_small;
    uint8_t mac_hex[6];
    
    /* Print chip information */
    esp_chip_info_t chip_info;
    uint32_t flash_size;
    esp_app_desc_t running_app_info;

    esp_chip_info(&chip_info);
    printf("This is %s chip with %d CPU core(s), %s%s%s%s, ",
           CONFIG_IDF_TARGET,
           chip_info.cores,
           (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "",
           (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "",
           (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "",
           (chip_info.features & CHIP_FEATURE_IEEE802154) ? ", 802.15.4 (Zigbee/Thread)" : "");

    unsigned major_rev = chip_info.revision / 100;
    unsigned minor_rev = chip_info.revision % 100; 
    printf("silicon revision v%d.%d, ", major_rev, minor_rev);
    if(esp_flash_get_size(NULL, &flash_size) != ESP_OK) {
        printf("Get flash size failed");
        return;
    }

    printf("%" PRIu32 "MB %s flash\n", flash_size / (uint32_t)(1024 * 1024),
           (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" : "external");

    // printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_free_heap_size());

    printf("This is a %s\n", DEVICE_NAME);
#ifdef PSRAM_EN
    printf("enable psram\n");
#endif

    const esp_partition_t *running = esp_ota_get_running_partition();
    if (esp_ota_get_partition_description(running, &running_app_info) == ESP_OK) {
        printf("Running firmware version: %s\n", running_app_info.version);
        strcpy(device_attr.software_version,running_app_info.version);
        sscanf(running_app_info.version, "v%d.%d.%d", &ver_big, &ver_middle, &ver_small);
        gatt_device_info[6] = ((ver_big & 0x0f) << 4) | (ver_middle & 0x0f);
    }

    ESP_ERROR_CHECK(esp_read_mac(mac_hex, ESP_MAC_BT));
    sprintf(device_attr.bt_mac, "%02x%02x%02x%02x%02x%02x", mac_hex[0], mac_hex[1], mac_hex[2],mac_hex[3], mac_hex[4], mac_hex[5]);
    ESP_LOGI(TAG,"read BT mac:%s", device_attr.bt_mac);
    memcpy(gatt_device_info, mac_hex, 6);
    strcpy(device_attr.device_mac, device_attr.bt_mac);
    printf("MAC address:%s\n", device_attr.device_mac);
}

void app_main(void)
{
    ESP_LOGI(TAG, "Startup..");
    printf("Minimum free heap size: %lu bytes\n", esp_get_minimum_free_heap_size());

    generic_gpio();
    system_get_info();
    nvs_init();
    led_strip_init();
    lamp_manage_init();
    touch_board_init();
    radar_init();
    uart_config_init();
    radar_set_en();
    user_timer_init(); 
    user_event_init();
}
